Colorblind fridenly plotting in R

在 R 中绘制色盲友好的图

Plot
Author

Jie Hua

Published

Aug 28, 2025

Modified

Aug 28, 2025

0. 基本信息 / Basic Information

要在 R 中使用色盲友好的调色板,主要需要以下几个包。请务必在保存图片之前,用这些包检查过图。谢谢!

To use colorblind-friendly palettes in R, you mainly need the following packages. Be sure that we checked the plots with these packages before saving the plots. Thank you!

1. 色盲模拟(colorblindr) / 1. colorbindr

在可用于生产的 R 图形中模拟色盲效果。作者:Claire D. McWhite 与 Claus O. Wilke。

Simulate colorblindness in production-ready R figures. Written by Claire D. McWhite and Claus O. Wilke.

官方仓库:https://github.com/clauswilke/colorblindr

Official repo: https://github.com/clauswilke/colorblindr

1.1 安装 / 1.1 Installation

Code
remotes::install_github("wilkelab/cowplot")
install.packages("colorspace", repos = "http://R-Forge.R-project.org")
remotes::install_github("clauswilke/colorblindr")

也可以通过以下方式安装:(但你需要先安装 pak。

It could also be installed with: (but you need to install pakfirst.

Code
pak::pkg_install("clauswilke/colorblindr")

1.2 演示 / 1.2 Demo

按照官方文档操作很容易,首先创建一个如下的 ggplot 对象:

It is easy to follow the official document, first make a ggplot object like:

Code
library(ggplot2)
fig <- ggplot(iris, aes(Sepal.Length, fill = Species)) + geom_density(alpha = 0.7)
fig

然后我们可以用这个包在不同的模拟条件下进行检查:

Then we could use this package to check in different simulations:

Code
library(colorblindr)
cvd_grid(fig)

也可以在交互式应用中查看它,这里我只展示脚本,它不会为读者打开新的标签页。

It is also possible to inspect it in the interactive app, I just show the script, it will not open a tab for readers.

Code
view_cvd(fig) # starts the interactive app

2. 调色板检查(colorblindcheck) / 2. colorblindcheck

检查调色板是否存在与色觉缺陷有关的问题

Check Color Palettes for Problems with Color Vision Deficiency

官方仓库:https://github.com/Nowosad/colorblindcheck

Official repo: https://github.com/Nowosad/colorblindcheck

2.1 安装 / 2.1 Installation

Code
install.packages("colorblindcheck")

2.2 演示 / 2.2 Demo

Code
library(colorblindcheck)
rainbow_pal = rainbow(n = 7)
rainbow_pal
[1] "#FF0000" "#FFDB00" "#49FF00" "#00FF92" "#0092FF" "#4900FF" "#FF00DB"
Code
#> [1] "#FF0000" "#FFDB00" "#49FF00" "#00FF92" "#0092FF" "#4900FF" "#FF00DB"
palette_check(rainbow_pal, plot = TRUE)

          name n tolerance ncp ndcp  min_dist mean_dist  max_dist
1       normal 7  12.13226  21   21 12.132257  61.06471 107.63470
2 deuteranopia 7  12.13226  21   19  2.572062  44.29065  85.87461
3   protanopia 7  12.13226  21   17  3.647681  47.63882  83.28286
4   tritanopia 7  12.13226  21   20  2.025647  47.41585  83.77189